package 文件io;

import java.io.*;
import java.nio.channels.*;
import java.nio.*;

/*
  copy速度对比 带缓冲的字节流最快
  nio的通道和映射要慢 不知道为什么
*/
public class 新io通道和映射
{
	public static void main(String[]args){
		String src="/storage/emulated/0/test.t";
		String target1="/storage/emulated/0/test1.t";
		String target2="/storage/emulated/0/test2.t";
		String target3="/storage/emulated/0/test3.t";
		String target4="/storage/emulated/0/test4.t";
		copy(src,target1);
		copyBuffer(src,target2);
		ChannelCopy(src,target3);
		RandamAccessFileCopy(src,target4);
		
	}
	//通道复制
	private static void ChannelCopy(String src,String target){
		System.out.println("通道复制 开始......");
		long start=System.currentTimeMillis();
		try
		{   //创建一个输入文件通道
			FileChannel fIn=new FileInputStream(src).getChannel();
			//创建一个输出文件通道
			FileChannel fOut=new FileOutputStream(target).getChannel();
			//创建字节缓冲区
			ByteBuffer buf=ByteBuffer.allocate(1024);
			while(fIn.read(buf)!=-1){
				buf.flip();//写入前要反转
				fOut.write(buf);
				buf.clear();//清除缓存
			}
			fIn.close();
			fOut.close();
		}
		catch (FileNotFoundException e)
		{}
		catch(IOException e){}
		long end=System.currentTimeMillis();
		System.out.println("通道复制完成，用时 "+(end-start)+"ms");
	}
	//RondamAccessFile，映射复制
	private static void RandamAccessFileCopy(String src,String target){
		long start=System.currentTimeMillis();
		System.out.println("映射开始复制.......");
		try
		{
			RandomAccessFile in=new RandomAccessFile(src, "r");
			RandomAccessFile out=new RandomAccessFile(target,"rw");
			
			FileChannel fcIn=in.getChannel();
			FileChannel fcOut=out.getChannel();
			
			long size=fcIn.size();//输入流的字节大小
			MappedByteBuffer inbuf=fcIn.map(FileChannel.MapMode.READ_ONLY,0,size);
			MappedByteBuffer outbuf=fcOut.map(FileChannel.MapMode.READ_WRITE,0,size);
			for(int i=0;i<size;i++){
				outbuf.put(inbuf.get());
			}
			//关闭通道时自动写入块
			fcIn.close();
			fcOut.close();
			in.close();
			out.close();
			
		}
		catch (FileNotFoundException e)
		{}
		catch(IOException e){}
		
		long end=System.currentTimeMillis();
		System.out.println("映射复制完成，用时 "+(end-start)+"ms");
	}
	//没套缓冲流的字节流复制
	private static void copy(String src,String target){
		long start=System.currentTimeMillis();
		System.out.println("字节流开始复制.......");
		File srcFile=new File(src);
		File targetFile=new File(target);
		OutputStream out=null;
		InputStream in=null;

		try
		{
			in = new FileInputStream(srcFile);
			out = new FileOutputStream(targetFile);

			byte[] bytes=new byte[1024];
			int len=-1;
			while ((len=in.read(bytes))!=-1){
				out.write(bytes,0,len);
			}
		}
		catch (FileNotFoundException  e)
		{}
		catch(IOException e){}
		finally{
			try
			{
				if (in != null)in.close();
				if (out != null)out.close();
			}
			catch (IOException e)
			{}
		}
		long end=System.currentTimeMillis();
		System.out.println("字节流复制完成，用时 "+(end-start)+"ms");
	}
	//加缓冲流的字节流复制
	//缓冲流底层已经改写利用了nio的缓存区机制了，所以缓冲流效率不低
	private static void copyBuffer(String src,String target){
		long start=System.currentTimeMillis();
		System.out.println("带缓冲字节流开始复制.......");
		File srcFile=new File(src);
		File targetFile=new File(target);
		OutputStream out=null;
		InputStream in=null;

		try
		{
			in = new FileInputStream(srcFile);
			out = new FileOutputStream(targetFile);
			BufferedInputStream bis=new BufferedInputStream(in);
			BufferedOutputStream bos=new BufferedOutputStream(out);
			byte[] bytes=new byte[1024];
			int len=-1;
			while ((len=bis.read(bytes))!=-1){
				bos.write(bytes,0,len);
			}
			bis.close();
			bos.close();
		}
		catch (FileNotFoundException  e)
		{}
		catch(IOException e){}
		
		long end=System.currentTimeMillis();
		System.out.println("带缓冲字节流复制完成，用时 "+(end-start)+"ms");
	}
}
